package com.superpeer.baseproject.activity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.superpeer.baseproject.R;
import com.superpeer.baseproject.adapter.LearnNewsAdapter;
import com.superpeer.baseproject.base.BaseActivity;
import com.superpeer.baseproject.bean.BaseBeanResult;
import com.superpeer.baseproject.utils.UpdateUtils;
import com.superpeer.baseproject.wight.ToastUitl;

import org.jetbrains.annotations.NotNull;

import butterknife.BindView;

public class MainActivity extends BaseActivity<MainPresenter,MainModel> implements MainContract.View {

    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private LearnNewsAdapter learnNewsAdapter;
    private int page = 1;
    private int pageSize = 10;

    @Override
    protected boolean hasHeadTitle() {
        return false;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this,mModel);
    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        learnNewsAdapter = new LearnNewsAdapter();
        recyclerView.setAdapter(learnNewsAdapter);

        mPresenter.getNews(page,pageSize);

        initListener();
        UpdateUtils.getInstance(this).update();
    }

    private void initListener() {
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                page++;
                mPresenter.getNews(page,pageSize);
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getNews(page,pageSize);
            }
        });
    }

    @Override
    public void showGetNews(BaseBeanResult baseBeanResult) {
        try {
            smartRefreshLayout.finishRefresh(true);
            smartRefreshLayout.finishLoadMore(true);
            if (null != baseBeanResult) {
                if ("1".equals(baseBeanResult.getCode())) {
                    if (null != baseBeanResult.getData()) {
                        if (page == 1) {
                            learnNewsAdapter.setNewInstance(baseBeanResult.getData().getList());
                        } else {
                            learnNewsAdapter.addData(baseBeanResult.getData().getList());
                        }
                        if(learnNewsAdapter.getData().size() == baseBeanResult.getData().getTotal()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }
                    }
                }else{
                    if(null != baseBeanResult.getMsg()){
                        ToastUitl.showShort(mContext,baseBeanResult.getMsg());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showLoading(String title) {

    }

    @Override
    public void stopLoading() {

    }

    @Override
    public void showErrorTip(String msg) {

    }
}